//
//  GPUImagePauseMovieWriter.m
//  YPGraphicEngine
//
//  Created by AxcLogo on 2022/7/8.
//

#import "GPUImagePauseMovieWriter.h"

@implementation GPUImagePauseMovieWriter{
    BOOL _isPause;
    //  BOOL _isAudioOn;
    CMTime _offset;
    CMTime _timeOffset;
    CMTime _last;
    BOOL _isDisCount;
    
}


-(void)newFrameReadyAtTime:(CMTime)frameTime atIndex:(NSInteger)textureIndex{
    if (_isPause) {
        return;
    }
    if (_isDisCount) {
        _isDisCount = NO;
        _offset = CMTimeSubtract(frameTime, _last);
        if (_offset.value > 0) {
            _timeOffset = CMTimeAdd(_timeOffset, _offset);
        }
    }
    _last = frameTime;
    frameTime = CMTimeSubtract(frameTime, _timeOffset);
    //  NSLog(@"_timeOffset->%lf, ->%lf", _timeOffset.value, _timeOffset.timescale);
    [super newFrameReadyAtTime:frameTime atIndex:textureIndex];
    
}

-(void)pause{
    _isPause = YES;
    
}

-(void)continueWrite{
    _isPause = NO;
    //  [self configure];
    
}

-(void)configure{
    _timeOffset = CMTimeMake(0, 2);
    
    _isDisCount = YES;
        
    _offset = kCMTimeZero;
    
}

-(void)processAudioBuffer:(CMSampleBufferRef)audioBuffer{
    if (_isPause) {
        return;
    }
    [super processAudioBuffer:audioBuffer];
    
}

@end


